package com.crr.mall.ware.service.impl;

import com.crr.mall.common.to.StockVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.ware.dao.WareSkuDao;
import com.crr.mall.ware.entity.WareSkuEntity;
import com.crr.mall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryByCondition(Map map) {
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(page, pageSize);
        List<Map> list = baseMapper.queryByCondition(map);
        PageInfo info = new PageInfo(list);
        return new PageUtils(list, (int) info.getTotal(), pageSize, page);

    }

    @Override
    public List<StockVO> queryStock(List<Long> skuIds) {

        List<StockVO> collect = skuIds.stream().map(skuId -> {
            StockVO stockVO = new StockVO();
            Long count = baseMapper.queryStock(skuId);
            stockVO.setSkuId(skuId);
            stockVO.setHasStock(count==null?false:count>0);
            return stockVO;
        }).collect(Collectors.toList());
        return collect;
    }

}